﻿using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Core;
using Michsky.UI.Heat;
using QFramework;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace Gp.Scripts.Core.GUI.Tutorial {
    public class TutorialWindowController : MonoBehaviour, IController {
        [SerializeField] private Image sprite;
        [SerializeField] private ButtonManager nextButton;
        [SerializeField] private ButtonManager lastButton;
        [SerializeField] private ModalWindowManager windowManager;
        [SerializeField] private VideoPlayer videoPlayer;
        [SerializeField] private List<TutorialInfoConfigs> tutorials;

        private LinkedListNode<TutorialInfoNode> _curNode = null;
        private LinkedList<TutorialInfoNode> _linkedList;

        private void Start() {
            nextButton.onClick.AddListener(ShowNext);
            lastButton.onClick.AddListener(ShowLast);
            windowManager.onConfirm.AddListener(OnConform);
            videoPlayer.isLooping = true;

            this.RegisterEvent<ShowTutorialWindowEventArgs>(SwitchTutorial);
            Close();
        }


        public void SwitchTutorial(ShowTutorialWindowEventArgs args) {
            var eventName = args.Key;
            _linkedList = new LinkedList<TutorialInfoNode>(
                tutorials
                .Where(x => x && x.eventName == eventName)
                .SelectMany(x => x.infoNodes)
            );
            _curNode = _linkedList.First;
            if (_curNode != null) {
                Show();
            }
        }


        private void ShowLast() {
            if (_curNode?.Previous == null) return;
            _curNode = _curNode.Previous;

            Show();
        }

        private void ShowNext() {
            if (_curNode?.Next == null) return;
            _curNode = _curNode.Next;

            Show();
        }


        [Button]
        private void Show() {
            if (_curNode == null) return;
            var info = _curNode.Value;
            windowManager.titleText = info.title;
            windowManager.descriptionText = info.description;
            windowManager.OpenWindow();
            windowManager.UpdateUI();

            

            if (info.videoShowCase != null) {
                sprite.Hide();
                videoPlayer.clip = info.videoShowCase;
                videoPlayer.Play();
            }
            else {
                sprite.sprite = info.imageShowCase;
                sprite.Show();
                videoPlayer.Stop();
                videoPlayer.clip = null;
            }
        }

        
        private void Close() {
            windowManager.CloseWindow();
        }


        private void OnConform() {
            if (_curNode?.Next == null) {
                Close();
            }
            else {
                ShowNext();
            }
        }
        

        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}